<template>
  <el-container class="main-container">
    <!-- 头部区域 -->
    <el-header>
      <!-- 左侧的 logo -->
      <img width="60" src="../assets/logo.jpg" alt="" class="logo" />
      <span style="color: white; font-size: '30px'">爱尔眼科后台管理系统</span>
      <!-- 右侧的菜单 -->
      <el-menu
        class="el-menu-top"
        mode="horizontal"
        background-color="#184996"
        text-color="#fff"
        active-text-color="#409EFF"
      >
        <el-submenu index="1">
          <template slot="title">
            <!-- 头像 -->
            <img src="@/assets/t1.png" alt="" v-if="menus" class="avatar" />
            <span>个人中心</span>
          </template>
          <el-menu-item index="1-1"
            ><i class="el-icon-s-operation"></i>基本资料</el-menu-item
          >
          <el-menu-item index="1-2"
            ><i class="el-icon-camera"></i>更换头像</el-menu-item
          >
          <el-menu-item index="1-3"
            ><i class="el-icon-key"></i>重置密码</el-menu-item
          >
        </el-submenu>
        <el-menu-item index="2" @click="logoutFn"
          ><i class="el-icon-switch-button"></i>退出</el-menu-item
        >
      </el-menu>
    </el-header>
    <el-container>
      <!-- 左侧边栏的用户信息 -->
      <el-aside width="200px">
        <div class="user-box">
          <img src="@/assets/t1.png" alt="" v-if="menus" />
          <!-- <img src="../../assets/images/head.png" alt="" v-else /> -->
          <span>欢迎 {{ nickname || name }}</span>
        </div>
        <!-- 左侧导航菜单 -->
        <el-menu
          :default-active="$route.path"
          class="el-menu-vertical-demo"
          background-color="#184996"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          router
        >
          <div class="color">
            <template v-for="item in data">
              <!-- 不包含子菜单的“一级菜单” -->
              <el-menu-item
                :index="item.indexPath"
                :key="item.indexPath"
                v-if="!item.children"
              >
                <i :class="item.icon"></i>{{ item.title }}
              </el-menu-item>

              <!-- 包含子菜单的“一级菜单” -->
              <el-submenu :index="item.indexPath" :key="item.indexPath" v-else>
                <template slot="title">
                  <i :class="item.icon"></i>
                  <span>{{ item.title }}</span>
                </template>
                <!-- 循环渲染“二级菜单” -->
                <el-menu-item
                  :index="subItem.indexPath"
                  v-for="subItem in item.children"
                  :key="subItem.indexPath"
                >
                  <i :class="subItem.icon"></i>{{ subItem.title }}
                </el-menu-item>
              </el-submenu>
            </template>
          </div>
        </el-menu>
      </el-aside>

      <el-container>
        <!-- 页面主体区域 -->
        <!-- 二级路由挂载点 -->
        <el-main>
          <router-view></router-view>
        </el-main>
        <!-- 底部 footer 区域 -->
        <el-footer>© WEB2205·Gong</el-footer>
      </el-container>
    </el-container>
  </el-container>
</template>

<script>
import { mapGetters, mapState } from "vuex";
import { getMenusAPI } from "@/api";
export default {
  mounted() {
    this.$store.dispatch("getuserInfoActions");
    console.log(this.$store.state.userinfo, "88**99**");
    this.userinfo = this.$store.state.userinfo;
    console.log("userinfo:", this.userinfo, "avator:", this.userinfo[0].avator);
  },
  name: "my-layout",
  computed: {
    ...mapGetters(["nickname", "name", "avator"]),
  },
  data() {
    return {
      userinfo: "",
      menus: [], // 侧边栏数据
      data: [
        {
          indexPath: "/home",
          title: "统计报表",
          icon: "el-icon-s-order",
          children: [
            {
              indexPath: "/index/table",
              title: "预约待处理表",
              icon: "el-icon-timer",
            },
            {
              indexPath: "/index/age",
              title: "眼疾病年龄统计图",
              icon: "el-icon-s-data",
            },
            {
              indexPath: "/index/room",
              title: "科室业务表",
              icon: "el-icon-notebook-2",
            },
          ],
        },
        {
          indexPath: "2",
          title: "用户管理",
          icon: "el-icon-user",
          children: [
            {
              indexPath: "/index/admin",
              title: "管理员信息",
              icon: "el-icon-s-custom",
            },
            {
              indexPath: "/index/yishi",
              title: "医师信息",
              icon: "el-icon-postcard",
            },
            {
              indexPath: "/index/userinfo",
              title: "客户信息",
              icon: "el-icon-document-copy",
            },
          ],
        },
        {
          indexPath: "3",
          title: "系统设置",
          icon: "el-icon-s-tools",
          children: [
            {
              indexPath: "/index/user",
              title: "用户信息",
              icon: "el-icon-collection",
            },
            {
              indexPath: "/index/pwd",
              title: "修改密码",
              icon: "el-icon-lock",
            },
          ],
        },
      ],
    };
  },
  methods: {
    // 退出登录
    logoutFn() {
      // 询问用户是否退出登录
      this.$confirm("您确认退出登录吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // TODO：执行退出登录的操作
          // 1. 清空 token
          this.$store.commit("updateToken", "");
          // 2. 跳转到登录页面
          this.$router.push("/");
        })
        .catch((err) => err);
    },
  },
};
</script>

<style lang="scss" scoped>
.color {
  background-color: #184996;
}
.logo {
  margin-left: 80px;
}
.main-container {
  position: relative;

  height: 100vh;
  .el-header,
  .el-aside {
    background-color: #184996;
  }
  .el-header {
    padding: 0;
    display: flex;
    justify-content: space-between;
  }
  .el-main {
    overflow-y: scroll;
    height: 0;
    background-color: #f2f2f2;
  }
  .el-footer {
    background-color: #eee;
    font-size: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

.avatar {
  border-radius: 50%;
  width: 35px;
  height: 35px;
  background-color: #fff;
  margin-right: 10px;
  object-fit: cover;
}

// 左侧边栏用户信息区域
.user-box {
  height: 70px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-top: 1px solid #000;
  border-bottom: 1px solid #000;
  user-select: none;
  img {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    background-color: #fff;
    margin-right: 15px;
    object-fit: cover;
  }
  span {
    color: white;
    font-size: 12px;
  }
}

// 侧边栏菜单的样式
.el-aside {
  .el-submenu,
  .el-menu-item {
    width: 200px;
    user-select: none;
    background-color: #f2f2f2 !important;
    color: #000 !important;
  }
}
</style>
